const db = wx.cloud.database();
var app = getApp();
Page({
  options: {
    addGlobalClass: true,
  },
  data: {
    avatarUrl: '',
    userInfo: {},
    userName: 'null',
    userClass: 'null',
    elements: [{
        id: '0',
        title: '练习错题',
        name: 'answer',
        icon: 'question',
        color: 'blue'
      },{
        id: '1',
        title: '查看错题',
        name: 'order',
        icon: 'like',
        color: 'red'
      }
    ]
  },

  onLoad: function(options) {
    this.setData({
      examid: options.exam
    })
    this.onGetOpenid();
  },
  goMenu: function (e) {
    switch (1 * e.currentTarget.dataset.id) {
      case 0:
        wx.navigateTo({
          url: '../note/note?exam='+this.data.examid,
        })
        break;
      case 1:
        wx.navigateTo({
          url: '../mistakes/mistakes?exam='+this.data.examid,
        })
        break;

    }
  },
  onGetOpenid: function() {
    let that = this;
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        let openid = res.result.openid;
        app.globalData.openid = res.result.openid;
        that.setData({
          openid
        },()=>{
          console.log('当前用户openid');
          console.log(openid);
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        wx.navigateTo({
          url: '../deployFunctions/deployFunctions',
        })
      }
    })
  }
})